<template>
  <!-- 未登录：显示登录页 -->
  <router-view v-if="!isLoggedIn" @login-success="onLoginSuccess" />

  <!-- 已登录：显示整个已登录布局 -->
  <router-view v-else />
</template>

<script setup>
import { computed } from 'vue'
import { useUserStore } from '@/stores/user'
import { setAuthToken } from '@/utils/auth'

const userStore = useUserStore()
const isLoggedIn = computed(() => userStore.isLoggedIn)

function onLoginSuccess(res) {
  const { token, username, userId, permissions } = res.data.data
  // 设 token 到 request.js 实例
  setAuthToken(token)
  // 更新 store
  userStore.loginSuccess({ username, userId, permissions, token })
}
</script>
